import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:opene/home/authorize_bean.dart';
import 'package:opene/net/network_manager.dart';
import 'package:opene/net/response/base_response.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../JhPickerTool.dart';
import '../app_constant.dart';
import '../app_theme.dart';
import '../app_utils.dart';
import '../login/login.dart';
import '../ui/version.dart';

class MeScreen extends StatefulWidget {
  @override
  _MeScreenPageState createState() => _MeScreenPageState();
}

class _MeScreenPageState extends State<MeScreen> {
  String username = "";
  String location = "";

  @override
  void setState(VoidCallback fn) {
    super.setState(fn);

    print('QQQQQQQQQQQ>>>>>>' + location);
  }

  @override
  void initState() {
    super.initState();
    Utils.getSharedPreferenceString(Constants.username).then((value) => {
          if (username == '') {_updateUserName(value)}
        });

    Utils.getSharedPreferenceString(Constants.location).then((value) => {
          if (location == '') {_updateLocation(value)}
        });
  }

  _updateUserName(String value) {
    setState(() {
      username = value ?? '';
      print('username::' + username);
    });
  }

  _updateLocation(String value) {
    setState(() {
      location = value ?? '';
      print('warehouse::' + location);
      Constants.locationValue = location;
      grayscale();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 15, top: 15, right: 15, bottom: 0),
      color: MyAppTheme.background,
      child: Column(
        // This next line does the trick.
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.circular(8.0),
            child: Container(
              color: Colors.white,
              height: 80.0,
              child: Row(
                children: [
                  Image.asset('assets/images/ic_logo.jpg'),
                  Flexible(
                    flex: 1,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Text(username, style: MyAppTheme.body2),
                        GestureDetector(
                          child: Text(location, style: MyAppTheme.body1),
                          onTap: () {
                            Utils.getSharedPreferenceStringList(Constants.warehouses).then((warehouses) => {
                                  if (warehouses != null && warehouses.length > 0)
                                    {
                                      JhPickerTool.showStringPicker(context, data: warehouses, clickCallBack: (int index, var str) {
                                        print(index);
                                        print(str);

                                        _updateLocation(warehouses[index]);
                                      })
                                    }
                                });
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 15, right: 0, bottom: 0),
            child: GestureDetector(
              onTap: () {
                print('Version');
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => Version()),
                );
              },
              child: ClipRRect(
                borderRadius: BorderRadius.only(topLeft: Radius.circular(8.0), topRight: Radius.circular(8.0)),
                child: Container(
                  color: Colors.white,
                  height: 25.0,
                  padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                  child: Row(
                    children: const <Widget>[
                      Icon(
                        Icons.info_outline,
                        color: MyAppTheme.nearlyBlue,
                        size: 20,
                      ),
                      Expanded(
                        flex: 1,
                        child: Text("  Version", style: MyAppTheme.body2),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        size: 15,
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 1, right: 0, bottom: 0),
            child: ClipRRect(
              borderRadius: BorderRadius.only(topLeft: Radius.circular(0.0), topRight: Radius.circular(0.0)),
              child: Container(
                color: Colors.white,
                height: 25.0,
                padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.group,
                      color: MyAppTheme.nearlyBlue,
                      size: 20,
                    ),
                    Expanded(
                      flex: 1,
                      child: Text("  Add new employee", style: MyAppTheme.body2),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 1, right: 0, bottom: 0),
            child: ClipRRect(
              borderRadius: BorderRadius.only(topLeft: Radius.circular(0.0), topRight: Radius.circular(0.0)),
              child: Container(
                color: Colors.white,
                height: 25.0,
                padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.directions_run,
                      color: MyAppTheme.nearlyBlue,
                      size: 20,
                    ),
                    Expanded(
                      flex: 1,
                      child: Text("  Clock out", style: MyAppTheme.body2),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 1, right: 0, bottom: 0),
            child: ClipRRect(
              borderRadius: BorderRadius.only(topLeft: Radius.circular(0.0), topRight: Radius.circular(0.0)),
              child: Container(
                color: Colors.white,
                height: 25.0,
                padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.heart_broken,
                      color: MyAppTheme.nearlyBlue,
                      size: 20,
                    ),
                    Expanded(
                      flex: 1,
                      child: Text("  Heartbeat recording", style: MyAppTheme.body2),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 1, right: 0, bottom: 0),
            child: ClipRRect(
              borderRadius: BorderRadius.only(bottomLeft: Radius.circular(8.0), bottomRight: Radius.circular(8.0)),
              child: Container(
                color: Colors.white,
                height: 25.0,
                padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.vpn_key_outlined,
                      color: MyAppTheme.nearlyBlue,
                      size: 20,
                    ),
                    Expanded(
                      flex: 1,
                      child: Text("  Password", style: MyAppTheme.body2),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 10, right: 0, bottom: 0),
            child: ClipRRect(
              borderRadius: BorderRadius.only(topLeft: Radius.circular(8.0), topRight: Radius.circular(8.0)),
              child: Container(
                color: Colors.white,
                height: 25.0,
                padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.wifi,
                      color: MyAppTheme.nearlyBlue,
                      size: 20,
                    ),
                    Expanded(
                      flex: 1,
                      child: Text("  WIFI", style: MyAppTheme.body2),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 1, right: 0, bottom: 0),
            child: ClipRRect(
              borderRadius: BorderRadius.only(bottomLeft: Radius.circular(0.0), bottomRight: Radius.circular(0.0)),
              child: Container(
                color: Colors.white,
                height: 25.0,
                padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.chat,
                      color: MyAppTheme.nearlyBlue,
                      size: 20,
                    ),
                    Expanded(
                      flex: 1,
                      child: Text("  Chat", style: MyAppTheme.body2),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 1, right: 0, bottom: 0),
            child: ClipRRect(
              borderRadius: BorderRadius.only(bottomLeft: Radius.circular(8.0), bottomRight: Radius.circular(8.0)),
              child: Container(
                color: Colors.white,
                height: 25.0,
                padding: EdgeInsets.only(left: 5, top: 2, right: 5, bottom: 2),
                child: Row(
                  children: <Widget>[
                    Icon(
                      Icons.settings,
                      color: MyAppTheme.nearlyBlue,
                      size: 20,
                    ),
                    Expanded(
                      flex: 1,
                      child: Text("  Printer setup", style: MyAppTheme.body2),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 15,
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
            width: double.infinity,
            child: Text("UUID:XXX", style: MyAppTheme.body2),
          ),
          Container(
            height: 25.0,
            margin: const EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(8.0)),
              color: MyAppTheme.white,
              border: Border.all(
                color: MyAppTheme.nearlyBlue,
                width: 1.0,
              ),
            ),
            child: Text("connect printer", style: MyAppTheme.body2),
          ),
          Expanded(
            // 这个Expanded小部件会占据所有可用空间
            child: Container(),
          ),
          Container(
            height: 25.0,
            margin: const EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 0),
            alignment: Alignment.center,
            child: Row(
              children: <Widget>[
                Text("Password:213458", style: MyAppTheme.body2),
                Icon(
                  Icons.refresh,
                  color: MyAppTheme.nearlyBlue,
                  size: 20,
                ),
              ],
            ),
          ),
          GestureDetector(
            onTap: () {
              print('Logoff');
              Utils.logOut();
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(builder: (context) => Login()),
                (route) => route == null,
              );
            },
            child: Container(
              height: 25.0,
              margin: const EdgeInsets.only(left: 0, top: 5, right: 0, bottom: 5),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(8.0)),
                color: MyAppTheme.white,
                border: Border.all(
                  color: MyAppTheme.color_999999,
                  width: 1.0,
                ),
              ),
              child: Text("Logoff", style: MyAppTheme.body2),
            ),
          ),
        ],
      ),
    );
  }

  void grayscale() async {
    var sp = await SharedPreferences.getInstance();
    sp.setString(Constants.location, location);
    try {
      BaseResponse baseResponse = await NetworkManager().request(context, Constants.grayscale);

      if (baseResponse.status == 200) {
        JumpMapBean jumpMapBean = JumpMapBean.fromJson(baseResponse.data);
        if (jumpMapBean == null) {
          getJumpData();
          return;
        }
        setJumpData(jumpMapBean);
      } else {
        getJumpData();
      }
    } catch (e) {
      e.printStackTrace();
      getJumpData();
    }
  }

  Future<void> getJumpData() async {
    var sp = await SharedPreferences.getInstance();
    var string = sp.getString(Constants.versionClass);
    if (Utils.isEmpty(string)) {
      String jumpJson = Constants.jumpJson.replaceAll("\\\"", "\"");
      Constants.jumpMapBean = JumpMapBean.fromJson(json.decode(jumpJson));
    } else {
      Constants.jumpMapBean = JumpMapBean.fromJson(json.decode(string));
    }
  }

  Future<void> setJumpData(JumpMapBean data) async {
    String jumpJson = Constants.jumpJson.replaceAll("\\\"", "\"");
    Constants.jumpMapBean = JumpMapBean.fromJson(json.decode(jumpJson));

    if (Constants.jumpMapBean == null) {
      Constants.jumpMapBean = new JumpMapBean();
    }

    try {
      if (data.INBOUND_Checkin != null) {
        Constants.jumpMapBean.INBOUND_Checkin = (data.INBOUND_Checkin);
      }
      if (data.INBOUND_Tally != null) {
        Constants.jumpMapBean.INBOUND_Tally = (data.INBOUND_Tally);
      }
      if (data.INBOUND_Damage != null) {
        Constants.jumpMapBean.INBOUND_Damage = (data.INBOUND_Damage);
      }
      if (data.INBOUND_LPNPhoto != null) {
        Constants.jumpMapBean.INBOUND_LPNPhoto = (data.INBOUND_LPNPhoto);
      }
      if (data.INBOUND_Putaway != null) {
        Constants.jumpMapBean.INBOUND_Putaway = (data.INBOUND_Putaway);
      }
      if (data.OUTBOUND_B2C_Pickup != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_Pickup = (data.OUTBOUND_B2C_Pickup);
      }
      if (data.OUTBOUND_B2C_Label != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_Label = (data.OUTBOUND_B2C_Label);
      }
      if (data.OUTBOUND_B2C_OBLPN != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_OBLPN = (data.OUTBOUND_B2C_OBLPN);
      }
      if (data.OUTBOUND_B2C_Trailer != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_Trailer = (data.OUTBOUND_B2C_Trailer);
      }
      if (data.OUTBOUND_B2B_Pick != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_Pick = (data.OUTBOUND_B2B_Pick);
      }
      if (data.OUTBOUND_B2B_B2BLabel != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_B2BLabel = (data.OUTBOUND_B2B_B2BLabel);
      }
      if (data.OUTBOUND_B2B_PalletTag != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_PalletTag = (data.OUTBOUND_B2B_PalletTag);
      }
      if (data.OUTBOUND_B2B_Trailer != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_Trailer = (data.OUTBOUND_B2B_Trailer);
      }
      if (data.Container_Unload != null) {
        Constants.jumpMapBean.Container_Unload = (data.Container_Unload);
      }
      if (data.Container_Tally != null) {
        Constants.jumpMapBean.Container_Tally = (data.Container_Tally);
      }
      if (data.Container_Putatay != null) {
        Constants.jumpMapBean.Container_Putatay = (data.Container_Putatay);
      }
      if (data.Container_OBLPN != null) {
        Constants.jumpMapBean.Container_OBLPN = (data.Container_OBLPN);
      }
      if (data.Returns_Preprocess != null) {
        Constants.jumpMapBean.Returns_Preprocess = (data.Returns_Preprocess);
      }
      if (data.Returns_FinalProcess != null) {
        Constants.jumpMapBean.Returns_FinalProcess = (data.Returns_FinalProcess);
      }
      if (data.Returns_Putatay != null) {
        Constants.jumpMapBean.Returns_Putatay = (data.Returns_Putatay);
      }
      if (data.Returns_Trailer != null) {
        Constants.jumpMapBean.Returns_Trailer = (data.Returns_Trailer);
      }
      if (data.MANAGE_CycleCount != null) {
        Constants.jumpMapBean.MANAGE_CycleCount = (data.MANAGE_CycleCount);
      }
      if (data.MANAGE_Transfer != null) {
        Constants.jumpMapBean.MANAGE_Transfer = (data.MANAGE_Transfer);
      }
      if (data.MANAGE_Adjustment != null) {
        Constants.jumpMapBean.MANAGE_Adjustment = (data.MANAGE_Adjustment);
      }
      if (data.MANAGE_Merge != null) {
        Constants.jumpMapBean.MANAGE_Merge = (data.MANAGE_Merge);
      }
      if (data.Task_Damage != null) {
        Constants.jumpMapBean.Task_Damage = (data.Task_Damage);
      }

      String jsonStr = jsonEncode(Constants.jumpMapBean);
      var sp = await SharedPreferences.getInstance();
      sp.setString(Constants.versionClass, jsonStr);
    } catch (e) {
      e.printStackTrace();
    }
  }
}
